import 'package:crypto/crypto.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:search_people/utils/component/component.dart';
import 'package:search_people/utils/component/page_state/widgets/empty.dart';
import 'package:search_people/utils/component/selection/model.dart';
import 'package:search_people/utils/component/selection/widgets/tag.dart';
import 'package:search_people/value/topcolors.dart';

import 'index.dart';
import 'widgets/widgets.dart';

class SelectionPage extends StatelessWidget {
  SelectionPage({
    Key? key,
    required this.list,
    this.onSelected,
  }) : super(key: key) {
    controller.state.list = list;
    controller.state.onSelected = onSelected;
  }
  final Function(Map<int, List<SectionIndex>> indexs)? onSelected;
  final List<SelectionModel> list;
  final controller = Get.put(SelectionController());
  final state = Get.find<SelectionController>().state;
  // 主视图
  Widget _buildView() {
    List<SelectionModel> sl = [];
    List<SelectionModel> tl = [];
    if (list.length > state.firstIndex) {
      var md = list[state.firstIndex];

      sl = md.searchResult ?? md.children;
    }
    if (sl.length > (state.secondIndex ?? 0)) {
      tl = sl[state.secondIndex ?? 0].children;
    }
    return list.isEmpty
        ? Center(
            child: Container(
              width: 65.r,
              height: 65.r,
              decoration: BoxDecoration(
                color: Colors.grey[350],
                borderRadius: BorderRadius.circular(5.r),
              ),
              child: const CupertinoActivityIndicator(
                color: Colors.white,
              ),
            ),
          )
        : Row(
            children: [
              SizedBox(
                width: Get.width / 3,
                child: ListView(
                  children: list
                      .asMap()
                      .map(
                        (key, value) => MapEntry(
                          key,
                          SelectionAreaWidget(
                            md: value,
                            onTap: () {
                              controller.firstColumnChooseAction(
                                  idx: key, md: value);
                            },
                          ),
                        ),
                      )
                      .values
                      .toList(),
                ),
              ),
              Expanded(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Visibility(
                        visible:
                            list.isNotEmpty && list[state.firstIndex].canSearch,
                        child: Container(
                          margin: EdgeInsets.all(10.r),
                          height: 40.r,
                          padding: list[state.firstIndex].canSearchInput
                              ? EdgeInsets.zero
                              : EdgeInsets.symmetric(horizontal: 10.r),
                          decoration: BoxDecoration(
                              border: Border.all(color: TopColors.black_dd)),
                          child: list[state.firstIndex].canSearchInput
                              ? searchBar(
                                  onChanged: (p0) {
                                    controller.actionSearch(text: p0, list: sl);
                                  },
                                  hintText: list[state.firstIndex]
                                          .searchPlaceholder ??
                                      '搜索',
                                  height: 40.r,
                                  backgroundColor: Colors.transparent,
                                  alignment: Alignment.centerLeft,
                                  hintStyle: TextStyle(
                                    color: TopColors.black_99,
                                    fontSize: 13.sp,
                                  ),
                                  labelStyle: TextStyle(
                                    color: TopColors.black_33,
                                    fontSize: 13.sp,
                                  ),
                                  iconColor: TopColors.black_dd,
                                  borderRadius:
                                      const BorderRadius.all(Radius.zero),
                                )
                              : InkWell(
                                  onTap: () async {
                                    if (list[state.firstIndex].searchTap !=
                                        null) {
                                      var str = await list[state.firstIndex]
                                          .searchTap!();
                                      if (str.isNotEmpty) {
                                        for (var e1 in sl) {
                                          e1.isSelected = false;
                                          for (var e2 in e1.children) {
                                            e2.isSelected = false;
                                          }
                                        }
                                        var e = list[state.firstIndex];
                                        e.result = '${e.title}：$str';
                                        e.searchText = str;
                                        controller.update();
                                      }
                                    }
                                  },
                                  child: Row(
                                    mainAxisAlignment:
                                        MainAxisAlignment.spaceBetween,
                                    children: [
                                      Flexible(
                                        child: Text(
                                          list[state.firstIndex].searchText ??
                                              list[state.firstIndex]
                                                  .searchPlaceholder ??
                                              '搜索',
                                          maxLines: 1,
                                          overflow: TextOverflow.ellipsis,
                                          style: TextStyle(
                                              color: TopColors.black_99),
                                        ),
                                      ),
                                      Icon(
                                        Icons.chevron_right_rounded,
                                        color: TopColors.black_99,
                                        size: 26.r,
                                      )
                                    ],
                                  ),
                                ),
                        )),
                    Expanded(
                      child: Row(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          SizedBox(
                            width: Get.width /
                                3 *
                                (list[state.firstIndex].levelCount == 2
                                    ? 2
                                    : 1),
                            child: sl.isNotEmpty
                                ? list[state.firstIndex].showType ==
                                        SelectionItemType.list
                                    ? ListView(
                                        children: sl
                                            .asMap()
                                            .map(
                                              (key, value) => MapEntry(
                                                  key,
                                                  SelectionListWidget(
                                                    selectedIndex: controller
                                                            .state
                                                            .secondIndex ??
                                                        -1,
                                                    index: key,
                                                    md: value,
                                                    onTap: () {
                                                      controller
                                                          .secondColumnChooseAction(
                                                              idx: key,
                                                              md: value);
                                                    },
                                                    onChoosedTap: () {
                                                      controller
                                                          .secondColumnItemChooseAction(
                                                              idx: key,
                                                              md: value);
                                                    },
                                                  )),
                                            )
                                            .values
                                            .toList(),
                                      )
                                    : SelectionTagWidget(
                                        normalFontSize:
                                            sl.first.title.length < 7
                                                ? 13.sp
                                                : null,
                                        selectedFontSize:
                                            sl.first.title.length < 7
                                                ? 14.sp
                                                : null,
                                        tags: sl,
                                        onTap: (idxs, mds) {
                                          controller.tagItemChooseAction(
                                              idxs: idxs, mds: mds);
                                        },
                                      )
                                : Center(
                                    child: Container(
                                      alignment: Alignment.center,
                                      child: Text(
                                        '暂无数据',
                                        style: TextStyle(
                                          color: TopColors.black_33,
                                          fontSize: 13.sp,
                                        ),
                                      ),
                                    ),
                                  ),
                          ),
                          Visibility(
                              visible: list[state.firstIndex].levelCount >= 3,
                              child: SizedBox(
                                width: Get.width / 3,
                                child: tl.isNotEmpty
                                    ? list[state.firstIndex].showType ==
                                            SelectionItemType.list
                                        ? ListView(
                                            children: tl
                                                .asMap()
                                                .map(
                                                  (key, value) => MapEntry(
                                                    key,
                                                    SelectionListWidget(
                                                      selectedIndex: controller
                                                              .state
                                                              .thirdIndex ??
                                                          -1,
                                                      index: key,
                                                      md: value,
                                                      onTap: () {
                                                        controller
                                                            .thirdColumnChooseAction(
                                                                idx: key,
                                                                md: value);
                                                      },
                                                      onChoosedTap: () {
                                                        controller
                                                            .thirdColumnItemChooseAction(
                                                                idx: key,
                                                                md: value);
                                                      },
                                                    ),
                                                  ),
                                                )
                                                .values
                                                .toList(),
                                          )
                                        : SelectionTagWidget(
                                            tags: tl,
                                          )
                                    : Center(
                                        child: Container(
                                          alignment: Alignment.center,
                                          width: Get.width / 3,
                                          child: Text(
                                            '暂无数据',
                                            style: TextStyle(
                                              color: TopColors.black_33,
                                              fontSize: 13.sp,
                                            ),
                                          ),
                                        ),
                                      ),
                              )),
                        ],
                      ),
                    ),
                  ],
                ),
              ),
            ],
          );
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<SelectionController>(
      builder: (_) {
        return Scaffold(
          body: SafeArea(
            child: _buildView(),
          ),
          bottomNavigationBar: Column(
            mainAxisAlignment: MainAxisAlignment.end,
            mainAxisSize: MainAxisSize.min,
            children: [
              Container(
                  color: Colors.white,
                  alignment: Alignment.center,
                  padding:
                      EdgeInsets.symmetric(horizontal: 10.r, vertical: 8.r),
                  child: Column(
                    mainAxisSize: MainAxisSize.min,
                    crossAxisAlignment: CrossAxisAlignment.end,
                    children: [
                      Column(
                        children: list
                            .where((e) => e.result != null)
                            .map(
                              (e1) => Container(
                                padding: EdgeInsets.symmetric(vertical: 3.r),
                                alignment: Alignment.centerLeft,
                                child: Text(
                                  e1.result ?? '',
                                  overflow: TextOverflow.ellipsis,
                                  style: TextStyle(
                                      color: TopColors.black_33,
                                      fontSize: 13.sp),
                                ),
                              ),
                            )
                            .toList(),
                      ),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          //
                          InkWell(
                            onTap: controller.actionReset,
                            child: Container(
                              width: 80.r,
                              height: 40.r,
                              alignment: Alignment.center,
                              decoration: BoxDecoration(
                                  color: const Color(0xffB0C4DE),
                                  borderRadius: BorderRadius.circular(2.r)),
                              child: Text(
                                '重置',
                                style: TextStyle(
                                  color: TopColors.black_33,
                                  fontSize: 14.sp,
                                ),
                              ),
                            ),
                          ),
                          SizedBox(
                            width: 10.r,
                          ),
                          Expanded(
                              child: InkWell(
                            onTap: controller.actionSure,
                            child: Container(
                              height: 40.r,
                              alignment: Alignment.center,
                              decoration: BoxDecoration(
                                  color: const Color(0xff87CEFA),
                                  borderRadius: BorderRadius.circular(2.r)),
                              child: Text(
                                '确定',
                                style: TextStyle(
                                  color: Colors.white,
                                  fontSize: 14.sp,
                                ),
                              ),
                            ),
                          ))
                        ],
                      ),
                    ],
                  )),
            ],
          ),
        );
      },
    );
  }
}
